﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
using System.Linq;
using Newtonsoft.Json;
using T.FAS.Runtime.Expression.SPI;
using T.ServiceFramework.TaskCenter.Spi;
using T.FAS.Runtime.Expression.Entity;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Expression.Plugin
{
    public class TaskCenterExecutor : DynamicObject, IDynamicActionAdapter
    {
        /// <summary>
        /// JS脚本动态调用Dynamic必备函数
        /// 脚本写法：ConfigCenter.();
        /// </summary>
        /// <param name="name">动态调用DynamicObject中的函数名称</param>
        /// <param name="args">参数信息，参数类型必须增加 params</param>
        /// <returns></returns>
        public dynamic DynamicInvoke(string name, params object[] args)
        {
            return Execute(name, args);
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out dynamic result)
        {
            result = DynamicInvoke(binder.Name, args);
            return true;
        }

        /// <summary>
        /// 执行获取配置中心值的逻辑
        /// </summary>
        /// <param name="funcName"></param>
        /// <param name="paramList"></param>
        /// <returns></returns>
        private dynamic Execute(string funcName, object[] paramList)
        {
            if (string.Equals(funcName, "SendTask"))
            {
                return SendTask(paramList);
            }
            return null;
        }

        private dynamic SendTask(object[] paramList)
        {
            var param = ProcessInputParam(paramList);
            //ITaskSender sender = HSFService.Proxy<ITaskSender>();
            //IAsynTask task;
            //if (string.IsNullOrEmpty(param.MQConnectKey))
            //    task = sender.CreateAsynTask(param.TaskName, param.TaskBusinessType, param.RetryCount);
            //else
            //    task = sender.CreateAsynTaskToMQ(param.TaskName, param.TaskBusinessType, param.RetryCount, param.MQConnectKey);
            //if (param.Data != null)
            //{
            //    foreach (var key in param.Data.Keys)
            //    {
            //        task.Data.Add(key, param.Data[key]);
            //    }
            //}
            //sender.Send(task);
            return null;
        }

        private TaskSendEntity ProcessInputParam(object[] paramList)
        {
            if (paramList == null || paramList.Count() < 1)
                throw new Exception($"TaskSender调用参数异常");
            var paramStr = paramList[0].ToString();
            try
            {
                var entity = JsonConvert.DeserializeObject<TaskSendEntity>(paramStr);
                if (entity.RetryCount == 0)
                    entity.RetryCount = 1;
                return entity;
            }
            catch (Exception e)
            {
                throw new FasException(ExprExceptionCode.ErrorExpr, $"TaskSender参数反序列化异常", e);
            }
        }

        class TaskSendEntity
        {
            public string TaskName { get; set; }
            public string TaskBusinessType { get; set; }
            public int RetryCount { get; set; }
            public string MQConnectKey { get; set; }
            public Dictionary<object, object> Data { get; set; }
        }
    }
}
